package com.site.dao.impl.backend.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.stereotype.Repository;

import com.site.dao.backend.system.AuthDao;
import com.site.dto.backend.system.AuthorityBizBean;
import com.site.model.backend.system.AuthorityInfo;
import com.site.util.PropertyUtils;
import com.site.util.base.dao.BaseDaoImpl;
import com.site.util.base.dao.HQLHelper;
import com.site.util.extremetable.QueryPage;

@Repository
public class AuthDaoImpl extends BaseDaoImpl implements AuthDao {

	@Override
	public List<AuthorityInfo> getAuthList(QueryPage queryPage, AuthorityBizBean authorityBizBean) {
		HQLHelper hql = new HQLHelper("AuthorityInfo");
		if(null != authorityBizBean){
			if(!StringUtils.isEmpty(authorityBizBean.getName())){
				hql.addLike("name", authorityBizBean.getName());
			}
			if(null != authorityBizBean.getId()){
				hql.addEqual("id", authorityBizBean.getId());
			}
		}
		if(null != queryPage){
			return super.findByHQL(queryPage, hql.getHQL(),null,false);
		} else {
			return super.findByHQL(hql.getHQL(), null);
		}
	}

	@Override
	public AuthorityInfo getAuthByName(String name) {
		StringBuffer sql = new StringBuffer("SELECT a.id,a.name FROM m_system_authority a WHERE a.name = :name");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		List<Object[]> auths = this.findBySQL(sql.toString(),params);
		
		if(null == auths || auths.size()==0) {
			return null;
		} else {
			AuthorityInfo authorityInfo = new AuthorityInfo();
			 authorityInfo.setId(auths.get(0)[0] != null ? Long.parseLong(auths.get(0)[0]+""):null);
			 authorityInfo.setName(auths.get(0)[1] != null ? auths.get(0)[1].toString():null);
			 return authorityInfo;
		}
	}

	@Override
	public void save(AuthorityBizBean obj) {
		if(null != obj){
			AuthorityInfo po = new AuthorityInfo();
			PropertyUtils.copyProperties(po, obj);
			super.save(po);
		}
	}

	@Override
	public void update(AuthorityBizBean authorityBizBean) {
		if(null != authorityBizBean){
			AuthorityInfo po = (AuthorityInfo)super.getEntityById(AuthorityInfo.class, authorityBizBean.getId());
			if(null != po){
				PropertyUtils.copyNotNullProperties(po, authorityBizBean);
			}
			super.update(po);
		}
	}

	@Override
	public void disabled(String obj) {
		if(null != obj && obj.length() > 0){
			String sql = "update m_system_authority set MARK_DELETE='Y' where id in("+obj+")";
			this.getCountBySQL(sql);
		}
	}

	@Override
	public void enabled(String obj) {
		String sql = "update m_system_authority set MARK_DELETE='N' where id in("+obj+")";
		this.getCountBySQL(sql);
	}

	@Override
	public List<AuthorityInfo> getNotRoleAuthList(Long roleId,AuthorityBizBean vo, QueryPage queryPage) {
		StringBuffer sql = new StringBuffer("SELECT u.id,u.name,u.description,u.mark_delete FROM m_system_authority u WHERE u.MARK_DELETE = 'N'");
		Map<String, Object> params = new HashMap<String, Object>();
		if(null != vo){
			if(!StringUtils.isEmpty(vo.getName())){
				sql.append(" and u.NAME like %:name% ");
				params.put("name", vo.getName());
			}
		}
		sql.append(" AND u.id NOT IN(SELECT r.authority_id FROM m_system_role_authority r WHERE r.ROLE_ID = :roleId) order by u.id asc");
		List<AuthorityInfo> authorityInfoList =  null;
		
		if(null != roleId){
			
			params.put("roleId", roleId);
			
			List<Object[]> list=new ArrayList<Object[]>();
			
			if (queryPage == null) {
				list = super.findBySQL(sql.toString(),params);
			} else {
				list = super.findBySQL(queryPage, sql.toString(),params);
			}
			
			authorityInfoList = new ArrayList<AuthorityInfo>();
			for(Object[] object:list){
				AuthorityInfo po = new AuthorityInfo();
				po.setId(object[0] != null ? Long.parseLong(object[0].toString()):null);
				po.setName(object[1] != null ? object[1].toString():null);
				authorityInfoList.add(po);
			}
		}
		
		return authorityInfoList;
	}

}
